package com.jblank.common.config;

import net.sf.json.JSONObject;

import com.jblank.dto.DTO;

/**
 * 查询配置
 * @author kim
 * @since 2013-08-07
 */
public class QueryConfig {

	private String tableName;

	private String flds;

	private String condExp;

	private String condValues;
	
	private String cond;
	
	private String order;
	
	private int start;
	
	private int limit;


	public String getFlds() {
		return flds;
	}

	public void setFlds(String flds) {
		this.flds = flds;
	}

	public String getCondExp() {
		return condExp;
	}

	public void setCondExp(String condExp) {
		this.condExp = condExp;
	}

	public String getCondValues() {
		return condValues;
	}

	public void setCondValues(String condValues) {
		this.condValues = condValues;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getCond() {
		return cond;
	}

	public void setCond(String cond) {
		this.cond = cond;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	/**
	 * dto解析为config
	 * @param dto
	 * @return QueryConfig
	 * 以后可改为反射
	 */
	public static QueryConfig transDtoToConfig(DTO dto){
		QueryConfig queryConfig = null;
		if(dto != null && dto.get(GlobalConfig.QueryConfig) != null){
			queryConfig = new QueryConfig();
			JSONObject queryJson = JSONObject.fromObject(dto.getAsString(GlobalConfig.QueryConfig));
			if(queryJson.has("tableName") && queryJson.getString("tableName") != null){
				queryConfig.setTableName(queryJson.getString("tableName"));
			}if(queryJson.has("flds") && queryJson.getString("flds") != null){
				queryConfig.setFlds(queryJson.getString("flds"));
			}if(queryJson.has("cond") && queryJson.getString("cond") != null){
				queryConfig.setCond(queryJson.getString("cond"));
			}if(queryJson.has("condExp") && queryJson.getString("condExp") != null){
				queryConfig.setCondExp(queryJson.getString("condExp"));
			}if(queryJson.has("condValues") && queryJson.getString("condValues") != null){
				queryConfig.setCondValues(queryJson.getString("condValues"));
			}if(queryJson.has("order") && queryJson.getString("order") != null){
				queryConfig.setOrder(queryJson.getString("order"));
			}if(dto.getAsInteger("start") != null){
				queryConfig.setStart(dto.getAsInteger("start"));
			}if(dto.getAsInteger("limit") != null){
				queryConfig.setLimit(dto.getAsInteger("limit"));
			}if(queryJson.has("start") && queryJson.getString("start") != null){
				queryConfig.setStart(queryJson.getInt("start"));
			}if(queryJson.has("limit") && queryJson.getString("limit") != null){
				queryConfig.setLimit(queryJson.getInt("limit"));
			}
		}
		return queryConfig;
	}
}
